//
//  WmcHomeVC+Notice.swift
//  YPWatermarkCamera
//
//  Created by 赵洋 on 2022/8/24.
//

import UIKit
import YPBedrock

// MARK: - 扩展WmcHomeVC + WmcJMLinkActionProtocol

extension WmcHomeVC: WmcJMLinkActionProtocol {
    // 极光魔链收到通知
    func jmLinkReceived(with params: [AnyHashable: Any]) {
        if let brandShareCode = params["brand_share_code"] as? String { // 水印分享
            watermarkShare(code: brandShareCode)
        }else if let joinTeamCode = params["join_team_by_wechat_code"] as? String { // 团队邀请
            shareInvitationTeam(code: joinTeamCode)
        }
    }

    /// 分享团队
    func shareInvitationTeam(code: String) {
        WmcMediaTeamShareAPI.fetchshareWechatGetShareTeamInfo(code: code).subscribe(onNext: { [weak self] value in
            guard let weakSelf = self else { return }
            let dismissPresentVC = Self.PopTooRoot()
            weakSelf.showJoinTeamSheet(value, dismissPresentVC: dismissPresentVC)
        }, onError: { err in
            WmcHudManager.toast(err)
        }).disposed(by: rx.disposeBag)
    }

    /// 水印分享
    func watermarkShare(code: String) {
        WmcMediaWatermarkAPI.fetchWatermarkTemplate(code: code).subscribe(onNext: { [weak self] value in
            guard let weakSelf = self else { return }
            let dismissPresentVC = Self.PopTooRoot()
            weakSelf.showWatermarkShareAlert(value, dismissPresentVC: dismissPresentVC)
        }, onError: { err in
            WmcHudManager.toast(err)
        }).disposed(by: rx.disposeBag)
    }

    /// 返回首页
   static func PopTooRoot() -> Bool {
        var dismissPresentVC = false
        if let vc = YPApp.CurrentVC as? WmcBaseVC { // 获取当前VC
            if let nav = vc.navigationController {
                if let mainNavVC = nav.navigationController { // 有主要的导航控制器
                    mainNavVC.popToRootViewController(animated: true)
                } else { // 没有主要的，那自己就是主要的
                    nav.popToRootViewController(animated: true)
                }
            } else if let _ = vc as? WmcPresentVC {
                dismissPresentVC = true
            }
        }
        return dismissPresentVC
    }
}
